﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RMS.Web.Shell.Presenter;
using RMS.Web.Shell.View;
using RMS.Utils.MVP;
using RMS.Utils.Navigator;
using RMS.DataAccess.Entities;

namespace RMS
{
    public partial class PatientView : Page<PatientPresenter>, IPatientView
    {

        Patients _patients;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    _patients = this.Presenter.GetPatient(Convert.ToInt32(Request.QueryString[0].ToString().Decrypt(Constants.ApplicationKey)));
                    btnUpdate.Visible = true;
                    btnSave.Visible = false;
                    FillForm();
                }
                catch (Exception)
                {

                }
                FillddlContract();
            }
        }
        private void FillForm()
        {
            txtAddress.Text = _patients.Address;
            txtFamilyname.Text = _patients.FamilyName;
            dcBirthday.SelectedDate = _patients.Birthday;
            txtFirstname.Text = _patients.FirstName;
            txtMobile.Text = _patients.Mobile;
            txtNationality.Text = _patients.Nationality;
            txtPhone.Text = _patients.Phone;
            ddlCompanyName.SelectedValue = _patients.ContractID.ToString();
            if (!_patients.Gender)
                rdbFemale.Checked = true;

        }
        private void GetFormData()
        {
            _patients.Address = txtAddress.Text;
            _patients.Birthday = dcBirthday.SelectedDate;
            if (ddlCompanyName.SelectedValue != "")
                _patients.ContractID = Convert.ToInt32(ddlCompanyName.SelectedValue);
            _patients.FamilyName = txtFamilyname.Text;
            _patients.FirstName = txtFirstname.Text;
            _patients.Mobile = txtMobile.Text;
            _patients.Nationality = txtNationality.Text;
            _patients.Phone = txtPhone.Text;
            if (rdbMale.Checked)
                _patients.Gender = true;
            else
                _patients.Gender = false;
        }
        private void FillddlContract()
        {
            ddlCompanyName.DataSource = this.Presenter.GetContracts();
            ddlCompanyName.Items.Add(new ListItem("None", ""));
            ddlCompanyName.SelectedValue = "";
            ddlCompanyName.DataTextField = "CompanyName";
            ddlCompanyName.DataValueField = "ContractId";
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            _patients = this.Presenter.GetPatient(Convert.ToInt32(Request.QueryString[0].ToString().Decrypt(Constants.ApplicationKey)));
            GetFormData();
            this.Presenter.UpdatePatient(_patients);
            Notification.Show("Update Sussefuly");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            _patients = new Patients();
            GetFormData();
            this.Presenter.insertPatients(_patients);
            Notification.Show("Save Sussefuly");
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            btnUpdate.Visible = false;
            btnSave.Visible = true;
            txtAddress.Text = "";
            txtFamilyname.Text = ""; ;
            txtFirstname.Text = ""; ;
            txtMobile.Text = ""; ;
            txtNationality.Text = ""; ;
            txtPhone.Text = ""; ;
            ddlCompanyName.SelectedValue = "";
            dcBirthday.SelectedDate = DateTime.Now;
        }
    }
}